package com.zsk.util;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

import javax.servlet.http.HttpServletResponse;

import org.springframework.util.StringUtils;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class ResponseUtil {

	/**
	 * 设置二进制下载文件头(UTF-8编码格式)
	 * 
	 * @param response
	 * @param fileName
	 */
	public static void setDownloadHeader(HttpServletResponse response, String fileName) {
		try {
			fileName = new String(fileName.getBytes(), StandardCharsets.UTF_8);
			String urlFileName = URLEncoder.encode(fileName, StandardCharsets.UTF_8.name());
			response.setContentType("application/octet-stream;charset=UTF-8");
			response.setHeader("Content-Disposition", "attachment;filename=" + urlFileName);
			// response.addHeader("Pargam", "no-cache");
			// response.addHeader("Cache-Control", "no-cache");
		} catch (Exception e) {
			log.error(e.toString());
		}
	}

	public static void wirteFile(HttpServletResponse response, File file) {
		BufferedInputStream bis = null;
		byte[] buffer = new byte[1024];
		try {
			OutputStream os = response.getOutputStream();
			bis = new BufferedInputStream(new FileInputStream(file));
			int i = bis.read(buffer);
			while (i != -1) {
				os.write(buffer, 0, i);
				i = bis.read(buffer);
			}
			os.flush();
			os.close();
		} catch (Exception e) {
			log.error(e.toString());
		} finally {
			if (bis != null)
				try {
					bis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
	}

	public static void wirteFile(HttpServletResponse response, String str) {
		BufferedInputStream bis = null;
		byte[] buffer = new byte[1024];
		try {
			OutputStream os = response.getOutputStream();
			bis = new BufferedInputStream(new ByteArrayInputStream(str.getBytes("gbk")));
			int i = bis.read(buffer);
			while (i != -1) {
				os.write(buffer, 0, i);
				i = bis.read(buffer);
			}
			os.flush();
			os.close();
		} catch (Exception e) {
			log.error(e.toString());
		} finally {
			if (bis != null)
				try {
					bis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
	}

	/**
	 * response下载文件，浏览器自动识别文件名
	 * 
	 * @param response
	 * @param fileName
	 * @param file
	 */
	public static void writeFileToResponse(HttpServletResponse response, String fileName, File file) {
		if (file == null || !file.isFile()) {
			response.setStatus(HttpServletResponse.SC_NOT_FOUND);
			response.addHeader("message", "file is not exist");
			return;
		}
		byte[] buffer = new byte[1024];
		BufferedInputStream bis = null;
		try {
			if (StringUtils.isEmpty(fileName))
				fileName = file.getName();
			String urlFileName = URLEncoder.encode(fileName, StandardCharsets.UTF_8.name());
			response.setCharacterEncoding(StandardCharsets.UTF_8.name());
			response.setContentType("application/octet-stream");
			response.addHeader("Content-Disposition", "attachment;fileName=" + urlFileName);
			response.setHeader("Content-Length", String.valueOf(file.length()));
			response.addHeader("fileName", urlFileName);
			bis = new BufferedInputStream(new FileInputStream(file));
			OutputStream os = response.getOutputStream();
			int i = bis.read(buffer);
			while (i != -1) {
				os.write(buffer, 0, i);
				i = bis.read(buffer);
			}
			os.close();
			return;
		} catch (Exception e) {
			response.setStatus(HttpServletResponse.SC_EXPECTATION_FAILED);
			log.error("download " + fileName + " occured exception " + e.toString());
		} finally {
			try {
				if (bis != null)
					bis.close();
			} catch (IOException e) {
				log.error(e.toString());
			}
		}
	}
}
